﻿if (GBrowserIsCompatible()) {

    // Build up the map 
    var map = new GMap(document.getElementById("map"));
    //map.setCenter(new google.maps.LatLng(38.75617, -9.19644), 15);
    map.setCenter(new GLatLng(0, 0), 0);
    //map.addControl(new GLargeMapControl());
    //map.addControl(new GMapTypeControl());
    map.enableScrollWheelZoom();

//    var myIcon = new GIcon();
//    myIcon.image = '../content/images/markers/image.png';
//    myIcon.printImage = '../content/images/markers/printImage.gif';
//    myIcon.mozPrintImage = '../content/images/markers/mozPrintImage.gif';
//    myIcon.iconSize = new GSize(32, 32);
//    myIcon.shadow = '../content/images/markers/shadow.png';
//    myIcon.transparent = '../content/images/markers/transparent.png';
//    myIcon.shadowSize = new GSize(48, 32);
//    myIcon.printShadow = '../content/images/markers/printShadow.gif';
//    myIcon.iconAnchor = new GPoint(16, 32);
//    myIcon.infoWindowAnchor = new GPoint(16, 0);
//    myIcon.imageMap = [19, 0, 20, 1, 21, 2, 22, 3, 23, 4, 24, 5, 24, 6, 24, 7, 24, 8, 24, 9, 24, 10, 24, 11, 24, 12, 23, 13, 23, 14, 22, 15, 21, 16, 20, 17, 20, 18, 19, 19, 19, 20, 18, 21, 18, 22, 17, 23, 17, 24, 17, 25, 17, 26, 16, 27, 16, 28, 16, 29, 16, 30, 16, 31, 14, 31, 14, 30, 14, 29, 14, 28, 14, 27, 14, 26, 13, 25, 13, 24, 13, 23, 12, 22, 12, 21, 12, 20, 11, 19, 10, 18, 10, 17, 9, 16, 8, 15, 7, 14, 7, 13, 6, 12, 6, 11, 6, 10, 6, 9, 6, 8, 6, 7, 6, 6, 7, 5, 7, 4, 8, 3, 9, 2, 10, 1, 11, 0];
    
    var gmarkers = [];
    var htmls = [];
    var to_htmls = [];
    var from_htmls = [];
    var i = 0;
    var bounds = new GLatLngBounds();

    // A function to create the marker and set up the event window
    function createMarker(point, name, html) {
        var marker = new GMarker(point);
        //var markerOptions = { icon: myIcon, draggable: false };
        //var marker = new GMarker(point, markerOptions);

        // The info window version with the "to here" form open
        to_htmls[i] = html + '<br>Direcções: <b>Para aqui<\/b> - <a href="javascript:fromhere(' + i + ')">a Partir daqui<\/a>' +
           '<br>Endereço:<form action="http://maps.google.com/maps" method="get" target="_blank">' +
           '<input type="text" SIZE=40 MAXLENGTH=40 name="saddr" id="saddr" value="" /><br>' +
           '<INPUT value="Pedir direcções" TYPE="SUBMIT">' +
           '<input type="hidden" name="daddr" value="' + point.lat() + ',' + point.lng() +
        // "(" + name + ")" + 
           '"/>';
        // The info window version with the "to here" form open
        from_htmls[i] = html + '<br>Direcções: <a href="javascript:tohere(' + i + ')">Para aqui<\/a> - <b>a Partir daqui<\/b>' +
           '<br>Endereço:<form action="http://maps.google.com/maps" method="get"" target="_blank">' +
           '<input type="text" SIZE=40 MAXLENGTH=40 name="daddr" id="daddr" value="" /><br>' +
           '<INPUT value="Pedir direcções" TYPE="SUBMIT">' +
           '<input type="hidden" name="saddr" value="' + point.lat() + ',' + point.lng() +
        // "(" + name + ")" + 
           '"/>';
        // The inactive version of the direction info
        html = html + '<br>Direcções: <a href="javascript:tohere(' + i + ')">Para aqui<\/a> - <a href="javascript:fromhere(' + i + ')">a Partir daqui<\/a>';

        GEvent.addListener(marker, "click", function () {
            marker.openInfoWindowHtml(html);
        });
        gmarkers[i] = marker;
        htmls[i] = html;
        i++;
        return marker;
    }

    // This function picks up the click and opens the corresponding info window
    function myclick(i) {
        gmarkers[i].openInfoWindowHtml(htmls[i]);
    }

    // functions that open the directions forms
    function tohere(i) {
        gmarkers[i].openInfoWindowHtml(to_htmls[i]);
    }
    function fromhere(i) {
        gmarkers[i].openInfoWindowHtml(from_htmls[i]);
    }

    // Process the Json file
    processJson = function (doc) {
        var jsonData = eval('(' + doc + ')');

        // Plot the markers
        for (var i = 0; i < jsonData.markers.length; i++) {
            var point = new GLatLng(jsonData.markers[i].lat, jsonData.markers[i].lng);
            var marker = createMarker(point, jsonData.markers[i].label, jsonData.markers[i].html);
            map.addOverlay(marker);

            bounds.extend(point);

            //centra o mapa no ultimo marker
            map.setCenter(point, 15); 
            //map.setCenter(bound.getCenter());

        }
        if (i > 1) {
            map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds));
            map.panTo(bounds.getCenter());
        }

        //map.setZoom(map.getBoundsZoomLevel(bound));
        //map.setCenter(bound.getCenter());
    }

    // Read from the hidden value and fetch the Json file.
    var markerUrl = document.getElementById("MarkerUrl").value;
    //markerUrl = markerUrl.substring(0, markerUrl.lastIndexOf('/'));
    GDownloadUrl(markerUrl, processJson);
}

else {
    alert("Sorry, the Google Maps API is not compatible with this browser");
}